package com.itheima.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

//@Component
//@Aspect
@Slf4j
@Order(1)
public class TimeAspect {


    @Pointcut("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
    public void pt(){}


    //重复的逻辑
    @Around("pt()")
    public Object timeRecord(ProceedingJoinPoint joinPoint) throws Throwable {
        //通过连接点获取方法的各种信息
        //获取目标方法所在类型的名字
        String className = joinPoint.getTarget().getClass().getName();
        //获取目标方法的名称
        Signature signature = joinPoint.getSignature();
        String methodName = signature.getName();
        Class declaringType = signature.getDeclaringType();

        //获取实参
        String args = Arrays.toString(joinPoint.getArgs());


        //开始时间
        long t1 = System.currentTimeMillis();
        //洗菜，切菜，xxxx 准备工作

        //TODO 调用原始方法
        Object result = joinPoint.proceed(); //炒菜
        //结束时间
        //洗锅
        long t2 = System.currentTimeMillis();
        //log.info("花费时间：{} ms", (t2 - t1));
        log.error("{}.{}方法，执行时实参数是{},declaringType:{},执行时间为{}"
                ,className,methodName,args,declaringType,(t2-t1));

        return result;
    }

    @Before("pt()")
    public void beforeAdvice() {
        log.error("******beforeAdvice*******111111");
    }

    @After("pt()")
    public void afterAdvice() {
        log.error("******afterAdvice*******11111");

    }

    @AfterReturning("pt()")
    public void afterReturningAdvice() {
        log.error("******afterReturningAdvice*******1111");

    }

    @AfterThrowing("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
    public void afterThrowingAdvice() {
        log.error("******afterThrowingAdvice*******1111");
    }


    @Pointcut("@annotation(com.itheima.anno.Log)")
    public void pt2(){}


    @Before("pt2()")
    public void before() {
        System.out.println("注解方式定义切入点生效！！！！+++");
    }
}














